package edu.faytechcc.csc289.data;

import java.sql.*;
import javax.swing.*;

/**
 * Class gets resultsets for each table
 * 
 * Apr 13, 2014
 *
 * @author William Robles
 */
public class SimulacraDbConnection
{
    /**
     * getStores method connects to database and
     * selects
     * @return results 
     */
    public ResultSet getStores()
    {
        ResultSet results = null;
        try
        {
            String selectStatement
                    = "SELECT * FROM \"Stores\"";
            
            results = getData().executeQuery(selectStatement);
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null,
                    "ERROR: " + ex.getMessage(),
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(42);
        }
        return results;
    }
    
    /**
     * getInventory method connects to inventory database and
     * gathers all inventory
     * @return results 
     */
    public ResultSet getInventory()
    {
        ResultSet results = null;
        try
        {
            String selectStatement
                    = "SELECT * FROM \"Inventory\"";
            
            results = getData().executeQuery(selectStatement);
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null,
                    "ERROR: " + ex.getMessage(),
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(42);
        }
        return results;
    }
    
    /**
     * getLoginInfo method connects the Employees data base
     * @return 
     */
    public ResultSet getLoginInfo()
    {
        ResultSet results = null;
        try
        {
            String selectStatement
                    = "SELECT * FROM \"Employees\"";
            
            results = getData().executeQuery(selectStatement);
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null,
                    "ERROR: " + ex.getMessage(),
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(42);
        }
        return results;
        
    }
    
    private Statement getData()
    {   
        Statement stmt = null;
        
        String url = "jdbc:derby://75.178.25.182:1537/D:\\SimulacraDB\\"
                                                        + "SimulacraUsedGamesDB";
        try
        {
            Connection conn = DriverManager.getConnection(url, "BobB", 
                                                                "password1!");
            stmt = conn.createStatement(
                    ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null,
                    "ERROR: " + ex.getMessage(),
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(42);
        }
        
        return stmt;
    }
}
